//---------------------------------------------------------------------------

#include <vcl.h>
#pragma hdrstop

#include "GlassesFirmDlg.h"
#include "GlassesModelDlg.h"
#include "FormUtilities.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"

//---------------------------------------------------------------------------
__fastcall TfrmGlassesFirmDlg::TfrmGlassesFirmDlg(TComponent* Owner, TGlassesBridge * bridge)
        : TForm(Owner)
{
   TFormUtilities::LocateForm(( TForm*)Owner, this);

   firm = new TGlassesFirm();

   this->bridge = bridge;
   this->Caption = "Add new firm";

   models = new TStringList();
   models->Duplicates = dupAccept;

   SetButtonsState();

   firmID = 0;

   isUpdate = false;
}

//---------------------------------------------------------------------------
__fastcall TfrmGlassesFirmDlg::TfrmGlassesFirmDlg(TComponent* Owner, TGlassesBridge * bridge, __int64 id, AnsiString name, AnsiString address, AnsiString phone)
        : TForm(Owner)
{
   TFormUtilities::LocateForm(( TForm*)Owner, this);

   firm = new TGlassesFirm();
   
   this->bridge = bridge;
   this->Caption = "Update firm";

   models = new TStringList();
   models->Duplicates = dupAccept;

   firmID = id;
   edtFirm->Text = name;
   edtAddress->Text = address;
   edtPhone->Text = phone;
   bridge->GetModels( id, models);
   RefreshGrid();

   isUpdate = true;
}


void TfrmGlassesFirmDlg::RefreshGrid()
{
   TGlassesModel * model;
   int rowCount;
   int i;

   for ( i = 1; i < grdModels->RowCount; i++)
      grdModels->Rows[ i]->Clear();

   if ( models->Count <= 1)
      rowCount = 2;
   else
      rowCount = models->Count + 1;

   grdModels->RowCount = rowCount;

   for( int i = 0; i < models->Count; i++)
   {
      model = ( TGlassesModel*)models->Objects[ i];
      grdModels->Cells[ 0][ i + 1] = model->name;
      grdModels->Cells[ 1][ i + 1] = FloatToStr( model->price);
      grdModels->Cells[ 2][ i + 1] = IntToStr( model->bought);
      grdModels->Cells[ 3][ i + 1] = IntToStr( model->sold);
   }

   SetButtonsState();
}

//---------------------------------------------------------------------------
void __fastcall TfrmGlassesFirmDlg::btnOKClick(TObject *Sender)
{
   if( edtFirm->Text.Length() == 0)
   {
        MessageDlg( "Firm name should be entered !",
                    mtError,
                    TMsgDlgButtons() << mbOK,
                    0);
                    
        edtFirm->SetFocus();
        ModalResult = mrNone;
        return;
   }

   firm->id = firmID;
   firm->name = edtFirm->Text;
   firm->address = edtAddress->Text;
   firm->phone = edtPhone->Text;

   if ( isUpdate)
      bridge->Update( firm, models);
   else
      bridge->Add( firm, models);
}

//---------------------------------------------------------------------------

void TfrmGlassesFirmDlg::SetButtonsState()
{
   btnUpdate->Enabled = ( models->Count > 0);
   btnDelete->Enabled = ( models->Count > 0);
}

//---------------------------------------------------------------------------
void __fastcall TfrmGlassesFirmDlg::FormDestroy(TObject *Sender)
{
   int i;

   for( i = 0; i < models->Count; i++)
      delete models->Objects[ i];
   models->Clear();
   delete models;

   delete firm;
}
//---------------------------------------------------------------------------
void __fastcall TfrmGlassesFirmDlg::btnUpdateClick(TObject *Sender)
{
   TfrmGlassesModelDlg * glassesModelDlg;
   TGlassesModel * model;

   model = ( TGlassesModel *)models->Objects[ grdModels->Row - 1];

   glassesModelDlg = new TfrmGlassesModelDlg( this, models, model);

   try
   {
      if ( glassesModelDlg->ShowModal() == mrOk)
         RefreshGrid();
   }
   __finally
   {
      delete glassesModelDlg;
   }//try-finally   
}
//---------------------------------------------------------------------------

void __fastcall TfrmGlassesFirmDlg::btnAddClick(TObject *Sender)
{
   TfrmGlassesModelDlg * glassesModelDlg;

   glassesModelDlg = new TfrmGlassesModelDlg( this, models);

   try
   {
      if ( glassesModelDlg->ShowModal() == mrOk)
         RefreshGrid();
   }
   __finally
   {
      delete glassesModelDlg;
   }//try-finally
}
//---------------------------------------------------------------------------

void __fastcall TfrmGlassesFirmDlg::FormShow(TObject *Sender)
{
   grdModels->Cells[ 0][0] = "MODEL";
   grdModels->Cells[ 1][0] = "PRICE";
   grdModels->Cells[ 2][0] = "BOUGHT";
   grdModels->Cells[ 3][0] = "SOLD";
}
//---------------------------------------------------------------------------

void __fastcall TfrmGlassesFirmDlg::btnDeleteClick(TObject *Sender)
{
   if ( MessageDlg( "Are you sure ?",
                    mtWarning,
                    TMsgDlgButtons() << mbYes << mbNo,
                    0) == mrYes)
   {
      delete models->Objects[ grdModels->Row - 1];
      models->Delete( grdModels->Row - 1);
      RefreshGrid();
   }
}
//---------------------------------------------------------------------------


void __fastcall TfrmGlassesFirmDlg::grdModelsKeyDown(TObject *Sender,
      WORD &Key, TShiftState Shift)
{
   switch ( Key)
   {
      case VK_INSERT :
         btnAddClick( Sender);
         break;

      case VK_DELETE :
         if ( btnDelete->Enabled)
            btnDeleteClick( Sender);
         break;

      case VK_SPACE :
         if ( btnUpdate->Enabled)
            btnUpdateClick( Sender);
         break;
   }
}
//---------------------------------------------------------------------------


